#include "encodernctest.h"

#include <string>

#define private public
#include <newcode/encoder/EncoderGC.h>
#undef private

using namespace newcode::encoder;

void EncoderNCTest::initialasationTestCase()
{
    Encoder encoder;
    encoder.encode(String("abcdef"));
    QCOMPARE(encoder.initialDataSize, 6);
    QCOMPARE((char)encoder.data[0], 'a');
    QCOMPARE((char)encoder.data[5], 'f');
}

void EncoderNCTest::computeSizeInBarselsTestCase()
{
    QFETCH(QString, data);
    QFETCH(int, size);
    QFETCH(int, mode);
    string sData = string(data.toAscii().data(), data.size()) ;
    Encoder encoder;
    encoder.encode(sData, (EncodeMode)mode);
    QCOMPARE(encoder.sizeInBarsels, size);
}

void EncoderNCTest::computeSizeInBarselsTestCase_data()
{
    QTest::addColumn<QString>("data");
    QTest::addColumn<int>("size");
    QTest::addColumn<int>("mode");
    QTest::newRow("1") << "aaaa" << 12 << (int) ENCODE_MODE_2_LEVELS;
    QTest::newRow("2") << "aaaabbbbccc" << 16 << (int) ENCODE_MODE_2_LEVELS;
    QTest::newRow("3") << "aaaa" << 12 << (int) ENCODE_MODE_4_LEVELS;
    QTest::newRow("4") << "aaaabbbbccc" << 16 << (int) ENCODE_MODE_4_LEVELS;
    QTest::newRow("5") << "aaaa" << 12 << (int) ENCODE_MODE_8_LEVELS;
    QTest::newRow("6") << "aaaabbbbccc" << 12 << (int) ENCODE_MODE_8_LEVELS;
    QTest::newRow("7") << "aaaa" << 12 << (int) ENCODE_MODE_3_LEVELS;
    QTest::newRow("8") << "aaaabbbbccc" << 16 << (int) ENCODE_MODE_3_LEVELS;
    QTest::newRow("9") << "12345678901234567890123456789012345678901" <<
        20 << (int) ENCODE_MODE_8_LEVELS;
}

void EncoderNCTest::setupFormatDataTestCase()
{
    QFETCH(QString, data);
    QFETCH(int, mode);
    string sData = string(data.toAscii().data(), data.size()) ;
    Encoder encoder;
    encoder.encode(sData, (EncodeMode)mode);
    QCOMPARE((int) encoder.formatBytes[0], data.size());
    QCOMPARE((int) encoder.formatBytes[1], 0);
    QCOMPARE((encoder.formatBytes[2] & 0xC0) >> 6, mode);
    QCOMPARE(encoder.formatBytes[2] & 0x1, 1);
}

void EncoderNCTest::setupFormatDataTestCase_data()
{
    QTest::addColumn<QString>("data");
    QTest::addColumn<int>("mode");
    QTest::newRow("1") << "aaaa" << (int) ENCODE_MODE_2_LEVELS;
    QTest::newRow("2") << "aaaabbbbccc" << (int) ENCODE_MODE_8_LEVELS;
    QTest::newRow("3") << "aaaa" << (int) ENCODE_MODE_4_LEVELS;
    QTest::newRow("4") << "aaaaa" << (int) ENCODE_MODE_3_LEVELS;
}

void EncoderNCTest::barselForValueTest()
{
    QFETCH(int, value);
    QFETCH(int, mode);
    QFETCH(int, expected);

    Encoder encoder;
    encoder.encode(String("test"), (EncodeMode)mode);
    QCOMPARE((int)encoder.barselForValue(value), expected);
}

void EncoderNCTest::barselForValueTest_data()
{
    QTest::addColumn<int>("value");
    QTest::addColumn<int>("mode");
    QTest::addColumn<int>("expected");
    QTest::newRow("mode2_test1") << 0 << (int) ENCODE_MODE_2_LEVELS << 255;
    QTest::newRow("mode2_test2") << 1 << (int) ENCODE_MODE_2_LEVELS << 0;
    QTest::newRow("mode4_test1") << 0 << (int) ENCODE_MODE_4_LEVELS << 255;
    QTest::newRow("mode4_test2") << 1 << (int) ENCODE_MODE_4_LEVELS << 170;
    QTest::newRow("mode4_test3") << 2 << (int) ENCODE_MODE_4_LEVELS << 85;
    QTest::newRow("mode4_test4") << 3 << (int) ENCODE_MODE_4_LEVELS << 0;
    QTest::newRow("mode8_test1") << 0 << (int) ENCODE_MODE_8_LEVELS << 255;
    QTest::newRow("mode8_test2") << 1 << (int) ENCODE_MODE_8_LEVELS << 219;
    QTest::newRow("mode8_test3") << 2 << (int) ENCODE_MODE_8_LEVELS << 182;
    QTest::newRow("mode8_test6") << 5 << (int) ENCODE_MODE_8_LEVELS << 73;
    QTest::newRow("mode8_test7") << 6 << (int) ENCODE_MODE_8_LEVELS << 36;
    QTest::newRow("mode8_test8") << 7 << (int) ENCODE_MODE_8_LEVELS << 0;
    QTest::newRow("mode16_test1") << 0 << (int) ENCODE_MODE_3_LEVELS << 255;
    QTest::newRow("mode16_test8") << 1 << (int) ENCODE_MODE_3_LEVELS << 127;
    QTest::newRow("mode16_test16") << 2 << (int) ENCODE_MODE_3_LEVELS << 0;

}

